<template>
	<view class="wrapper">
		<view class="cu-form-group">
			<text class="title">*编号:</text><input v-model="NowSelectUnit.MemberCode" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<text class="title">*姓名:</text><input v-model="NowSelectUnit.MemberName" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<text class="title">*电话:</text><input v-model="NowSelectUnit.Photo" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">*性别:</view>
			<picker @change="IsSizeName" :value="IsSizeIndex" :range="IsSize">
				<view class="picker">
					{{IsSizeIndex>-1?IsSize[IsSizeIndex]:'点击选择'}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<text class="title">等级:</text><input v-model="NowSelectUnit.VipClass" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<text class="title">折扣:</text>
			<input type="number" v-model="NowSelectUnit.Discount" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<text class="title">积分:</text><input v-model="NowSelectUnit.integral" class="inpt"></input>
		</view>
		<view class="cu-form-group">
			<text class="title">余额:</text><input v-model="NowSelectUnit.Total" class="inpt"></input>
		</view>

		<view class="cu-form-group">
			<view class="title">*是否停用:</view>
			<picker @change="IsStopChange" :value="IsStopIndex" :range="IsStop">
				<view class="picker">
					{{IsStopIndex>-1?IsStop[IsStopIndex]:'点击选择'}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<button class="cu-btn stop text-white" @tap="add" :style="{visibility:type==1?'':'hidden'}">新增</button>
			<button class="cu-btn stop text-white" @tap="change" :style="{visibility:type==0?'':'hidden'}">修改</button>
		</view>
	</view>
</template>

<script>
	import * as Api from '@/syApis/syAddOrder/syaddApis.js';
	import * as Apis from '@/syApis/syDa/syDa.js';
	export default {
		data() {
			return {
               NowSelectUnit:{
				   MemberID:"",
				   MemberCode:"",
				   MemberName:"",
				   VipClass:"",
				   Phone:"",
				   Gender:"",
				   Discount:"",
				   integral:"",
				   Total:"",
				   IsStopIndex:"",
				   CreateID:""
			   },
				type: 0,
				IsStopIndex:-1,
				IsSizeIndex:0,
				IsStop:['否','是'],
				IsSize:['女','男'],
				
			}
		},
		onLoad(options) {
			let type = options.type;
			this.type = options.type;
			
			if (type == 0) {
				uni.setNavigationBarTitle({
					title: "修改会员信息"
				})
				this.NowSelectUnit =JSON.parse(options.NowSelectUnit);
				console.log('onload执行了')
				console.log(this.NowSelectUnit)
				this.IsStopIndex = this.NowSelectUnit.IsStop == false ? 0 : 1;
				this.IsSizeIndex = this.NowSelectUnit.Gender == 1 || this.NowSelectUnit.Gender == "男"?1:0;
			} else {
				uni.setNavigationBarTitle({
					title: "新增会员信息"
				})
			}
			this.GetOperatorMessage();
		},
		methods: {
			change() {
				let CreateID = uni.getStorageSync("UserCode");

				if (this.IsStopIndex == -1) {
					Apis.showTip("请选择是否停用");
					return;
				}
				if((this.NowSelectUnit.Discount+'').indexOf(".")>-1){
					 Apis.showTip("折扣只能为整数");
					 return;
				}
				let Save_M_Data_Json = {
					MemberID: this.NowSelectUnit.MemberID+"",
					MemberCode: this.NowSelectUnit.MemberCode + "",
					MemberName: this.NowSelectUnit.MemberName + "",
					VipClass: this.NowSelectUnit.VipClass + "",
					Photo: this.NowSelectUnit.Photo + "",
					Gender: this.NowSelectUnit.Gender + "",
					Discount: this.NowSelectUnit.Discount + "",
					integral: this.NowSelectUnit.integral + "",
					Total: this.NowSelectUnit.Total + "",
					IsStop: this.IsStopIndex + "",
					CreateID: CreateID + ""
				};
				let data = {
					Save_M_Data_Json: JSON.stringify(Save_M_Data_Json)
				}
				var that = this;
				Apis.Update_MemberMessage(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg != '') {
							Api.showTip(res.data.ErrorMsg);
						} else if (res.data.ErrorMsg == '获取登录信息失败') {

						} else {
							let data = {};
							that.$eventHub.$emit('fires',data);
							Api.showTip("修改成功");
						}
					} else {}
				})
			},
			//新增
			add() {
				let CreateID = uni.getStorageSync("UserCode");
				if(this.NowSelectUnit.MemberCode==""){
					Apis.showTip("编号不能为空");
					return;
				}
				if(this.NowSelectUnit.MemberName==""){
					Apis.showTip("姓名不能为空");
					return;
				}
				if(this.NowSelectUnit.Photo==""){
					Apis.showTip("电话不能为空");
					return;
				}
				
				if (this.IsStopIndex == -1) {
					Apis.showTip("请选择是否停用");
					return;
				}
				
				if((this.NowSelectUnit.Discount+'').indexOf(".")>-1){
					 Apis.showTip("折扣只能为整数");
					 return;
				}
				
				let Save_M_Data_Json = {
					MemberCode: this.NowSelectUnit.MemberCode + "",
					MemberName: this.NowSelectUnit.MemberName + "",
					VipClass: this.NowSelectUnit.VipClass + "",
					Photo: this.NowSelectUnit.Photo + "",
					Gender: this.NowSelectUnit.Gender + "",
					Discount: this.NowSelectUnit.Discount + "",
					integral: this.NowSelectUnit.integral + "",
					Total: this.NowSelectUnit.Total + "",
					IsStop: this.IsStopIndex + "",
					CreateID: CreateID + ""
				};
				let data = {
					Save_M_Data_Json: JSON.stringify(Save_M_Data_Json)
				}
				var that = this;
				Apis.Add_MemberMessage(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg != '') {
							Api.showTip(res.data.ErrorMsg);
						} else if (res.data.ErrorMsg == '获取登录信息失败') {

						} else {
							let data = {};
							that.$eventHub.$emit('fires',"");
							Api.showTip("新增成功");
						}
					} else {

					}
				})
			},
			
			IsSizeName(e) {
				if (e.detail.value == -1) {
					this.IsSizeIndex = 0;
					this.NowSelectUnit.Gender = this.IsSizeIndex;
				} else {
					this.IsSizeIndex = e.detail.value;
					this.NowSelectUnit.Gender = this.IsSizeIndex;
				}
			},
			
			IsStopChange(e) {
				if (e.detail.value == -1) {
					this.IsStopIndex = 0;
				} else {
					this.IsStopIndex = e.detail.value;
				}
			},
			//获取业务员列表
			GetOperatorMessage() {
				let data = {
					Filter: ""
				}
				this.optionsPicker = [];
				var that = this;
				Api.GetOperatorMessage(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg == "获取登录信息失败") {
							Api.showTip("登录超时");
						} else if (res.data.ErrorMsg != "") {
							Api.showTip(res.data.ErrorMsg);
						} else {
							let responseData = res.data.ResultMsg;
							that.optionsList = responseData;
							responseData.forEach((item, index) => {
								if (that.NowSelectUnit.EmpID == item.OperatorID) {
									that.optionIndex = index;
								}
								that.optionsPicker.push(item.OperatorName);
							});
						}
					} else {

					}
				});
			}
		}
	}
</script>

<style>
   .stop{
	   background: rgb(124,170,216) !important;
   }
</style>
